package com.bank.frame;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

import com.bank.dao.AdminDAO;
import com.bank.dao.UserDAO;
import com.bank.pojo.User;
import com.bank.utils.SystemConstants;

//登录面板
public class LoginPanel extends JPanel {
	public LoginPanel() {
		this.setBounds(0, 0, SystemConstants.FRAME_WIDTH, SystemConstants.FRAME_HEIGHT);
		SpringLayout springLayout = new SpringLayout();
		this.setLayout(new BorderLayout());

		//设置标题
		JLabel topNameLabel = new JLabel("银行账号管理系统",JLabel.CENTER);
		//设置标题字体和大小
		topNameLabel.setFont(new Font("宋体",Font.PLAIN,40));
		topNameLabel.setPreferredSize(new Dimension(0,80));
		this.add(topNameLabel,BorderLayout.NORTH);

		//设置中心内容
		//创建一个中间面板
		JPanel centerJPanel = new JPanel(springLayout); //中间使用弹簧布局
		Font font = new Font("楷体",Font.PLAIN,20);
		//用户名
		JLabel nameLabel = new JLabel("用户名");
		nameLabel.setFont(font);
		JTextField nameTextField = new JTextField();
		nameTextField.setPreferredSize(new Dimension(200,30));
		centerJPanel.add(nameLabel);
		centerJPanel.add(nameTextField);

		//密码
		JLabel pwdLabel = new JLabel("密码");
		pwdLabel.setFont(font);
		JPasswordField pwdField = new JPasswordField();
		pwdField.setPreferredSize(new Dimension(200,30));
		centerJPanel.add(pwdLabel);
		centerJPanel.add(pwdField);

		//登录按钮
		JButton loginBtn = new JButton("登录");
		centerJPanel.add(loginBtn);

		//权限按钮
		JRadioButton adminRadio = new JRadioButton("银行柜员", true);
		JRadioButton commonRadio = new JRadioButton("普通用户");
		ButtonGroup group = new ButtonGroup();// 创建一个按钮组
		group.add(adminRadio);
		group.add(commonRadio);
		centerJPanel.add(adminRadio);
		centerJPanel.add(commonRadio);

		this.add(centerJPanel,BorderLayout.CENTER);//将中心面板加入到主面板中

		//设置中心面板中的弹簧分部
		//  1. 布局 userNameLabel  (是下方组件的参照物)
		Spring nameLabelWidth = Spring.width(nameLabel);
		Spring nameTextFieldWidth = Spring.width(nameTextField);
		Spring springWidth = Spring.constant(20);  //左右间距20
		Spring childWidth = Spring.sum(Spring.sum(nameLabelWidth, nameTextFieldWidth), springWidth);
		int offsetX = childWidth.getValue() / 2;  //得到水平偏移量

		springLayout.putConstraint(SpringLayout.WEST, nameLabel, -offsetX, SpringLayout.HORIZONTAL_CENTER, centerJPanel);
		springLayout.putConstraint(SpringLayout.NORTH, nameLabel, 20, SpringLayout.NORTH, centerJPanel);

		//  2. userNameTextField
		springLayout.putConstraint(SpringLayout.WEST, nameTextField, 5, SpringLayout.EAST, nameLabel);
		springLayout.putConstraint(SpringLayout.NORTH, nameTextField, 0, SpringLayout.NORTH, nameLabel);

		//  3. passwordLabel
		springLayout.putConstraint(SpringLayout.EAST, pwdLabel, 0 , SpringLayout.EAST, nameLabel);
		springLayout.putConstraint(SpringLayout.NORTH, pwdLabel, 20, SpringLayout. SOUTH, nameLabel);

		//  4. passWordTextField
		springLayout.putConstraint(SpringLayout.WEST, pwdField, 5, SpringLayout.EAST, pwdLabel);
		springLayout.putConstraint(SpringLayout.NORTH, pwdField, 0, SpringLayout.NORTH, pwdLabel);

		//  5. PermissionButton
		springLayout.putConstraint(SpringLayout.WEST, adminRadio, 40, SpringLayout.WEST, pwdLabel);
		springLayout.putConstraint(SpringLayout.NORTH, adminRadio, 40, SpringLayout.SOUTH, pwdLabel);
		springLayout.putConstraint(SpringLayout.WEST, commonRadio, 60, SpringLayout.EAST, adminRadio);
		springLayout.putConstraint(SpringLayout.NORTH, commonRadio, 0, SpringLayout.NORTH, adminRadio);

		//  6. loginButton
		springLayout.putConstraint(SpringLayout.WEST, loginBtn, 100, SpringLayout.WEST, pwdLabel);
		springLayout.putConstraint(SpringLayout.NORTH, loginBtn, 100, SpringLayout.SOUTH, pwdLabel);

		//对按钮进行鼠标监视
		loginBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String username = nameTextField.getText();
				String password = new String(pwdField.getPassword());
				User user = null;
				JPanel panel = null;

				if (adminRadio.isSelected()) {
					//登录银行工作人员
					user = AdminDAO.getLoginUser(username);
					panel = new AdminPanel();//工作显示面板
				} else if (commonRadio.isSelected()) {
					//登录普通用户
					user = UserDAO.getUser(username);
					panel = new com.bank.frame.UserPanel();//用户显示面板
				}
				if (user == null || !user.getPassword().equals(password)) {
					JOptionPane.showMessageDialog(loginBtn.getParent(), "用户名或密码错误", "系统提示",
							JOptionPane.WARNING_MESSAGE);
				} else {
					com.bank.frame.MainFrame.user = user;
					com.bank.frame.MainFrame.setContent(panel);
				}

			}
		});

	}
}
